package per.dg.tencent.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import per.dg.tencent.R;
import per.dg.tencent.databinding.ActivityMainBinding;
import per.dg.tencent.fragment.HomeFragment;
import per.dg.tencent.viewmodel.MianActivityViewModel;

public class MainActivity extends AppCompatActivity {

    MianActivityViewModel mMianActivityViewModel;

    ActivityMainBinding mActivityMainBinding;

    List<Fragment> mList;

    HomeFragment mHomeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMianActivityViewModel = new ViewModelProvider(this).get(MianActivityViewModel.class);
        mActivityMainBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
//        setContentView(R.layout.activity_main);
        setContentView(mActivityMainBinding.getRoot());

//        findViewById(R.id.app_img_0).setOnClickListener(mOnClickListener);
//        findViewById(R.id.app_img_1).setOnClickListener(mOnClickListener);
//        findViewById(R.id.app_img_2).setOnClickListener(mOnClickListener);
//        findViewById(R.id.app_img_3).setOnClickListener(mOnClickListener);
//        findViewById(R.id.app_img_4).setOnClickListener(mOnClickListener);
        mActivityMainBinding.appImg0.setOnClickListener(mOnClickListener);
        mActivityMainBinding.appImg1.setOnClickListener(mOnClickListener);
        mActivityMainBinding.appImg2.setOnClickListener(mOnClickListener);
        mActivityMainBinding.appImg3.setOnClickListener(mOnClickListener);
        mActivityMainBinding.appImg4.setOnClickListener(mOnClickListener);

        mList = new ArrayList<>();

        mHomeFragment = HomeFragment.newInstance();
        mList.add(mHomeFragment);
        changeFragment(0);
    }

    private void changeFragment(int position){
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        transaction.replace(R.id.app_fm_main, mList.get(position));
        transaction.commit(); // 提交创建Fragment请求
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.app_img_0:
                    break;
                case R.id.app_img_1:
                    changeFragment(1);
                    break;
                case R.id.app_img_2:
                    break;
                case R.id.app_img_3:
                    break;
                case R.id.app_img_4:
                    break;
            }
        }
    };
}